package com.wingsfrog.ZcVideo;

import java.util.ArrayList;
import java.util.List;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.wingsfrog.genius_rc.R;
import com.wingsfrog.ZcVideo.PlayNode;

public class PlayDeviceAdapter extends BaseAdapter {
	Context context;
	private List<PlayNode> nodeList;
	private LayoutInflater inflater;
	private Bitmap[] listMap;
	int currentNodeId;
//	Drawable defaultImg;
	int colorSelector;
	int colorNormal;

	// Map<String, SoftReference<Bitmap>> softMap = new HashMap<String,
	// SoftReference<Bitmap>>();

	public PlayDeviceAdapter(Context context) {
		this.context = context;
		this.nodeList = new ArrayList<PlayNode>();
		// listMap = new Bitmap[nodeList.size()];
		inflater = LayoutInflater.from(context);
//		defaultImg = context.getResources().getDrawable(R.drawable.default_img);
//		this.colorNormal = context.getResources().getColor(R.color.select_item_bg);
//		this.colorSelector = context.getResources().getColor(R.color.select_item_bg_h);
	}

	public synchronized void addBitmap(int id, Bitmap map) {
		if(listMap==null)
			listMap=new Bitmap[nodeList.size()];
		
		listMap[id] = map;

	}

	// public synchronized void addBitmap(String id, Bitmap map) {
	// if (map != null) {
	// softMap.put(id, new SoftReference<Bitmap>(map));
	// }
	//
	// }
	//
	// public synchronized SoftReference<Bitmap> getBitmap(String id) {
	//
	// return softMap.get(id);
	//
	// }

	public List<PlayNode> getNodeList() {
		return nodeList;
	}

	public void setNodeList(List<PlayNode> nodeList) {
		this.nodeList = nodeList;
		if (listMap == null) {
			listMap = new Bitmap[nodeList.size()];
		}
		Bitmap[] listTemp = new Bitmap[nodeList.size()];
		for (int i = 0; i < listTemp.length; i++) {
			if (i == listMap.length) {
				break;
			}
			if (listMap[i] != null) {
				listTemp[i] = listMap[i];
			}
		}
		listMap = listTemp.clone();
		notifyDataSetChanged();
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return nodeList.size();
	}

	@Override
	public Object getItem(int arg0) {
		// TODO Auto-generated method stub
		return arg0;
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	public int getCurrentNodeId() {
		return currentNodeId;
	}

	public void setCurrentNodeId(int currentNodeId) {
		if (this.currentNodeId == currentNodeId) {
			return;
		}
		this.currentNodeId = currentNodeId;
		notifyDataSetChanged();
	}

	private ViewHolder vh = null;

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub

		if (convertView == null) {
			vh = new ViewHolder();
			convertView = inflater.inflate(R.layout.layout_play_device_item, null);
			vh.tv = (TextView) convertView.findViewById(R.id.show_name);
			vh.img = (ImageView) convertView.findViewById(R.id.item_img);
			vh.img_state = (ImageView) convertView.findViewById(R.id.item_img_state);
			convertView.setTag(vh);
		} else {
			vh = (ViewHolder) convertView.getTag();
		}
		PlayNode node = nodeList.get(position);
		// if (listMap[position] != null) {
		// vh.img.setImageBitmap(listMap[position]);
		// } else {
		// vh.img.setImageDrawable(defaultImg);
		// }
		// vh.img.setOnClickListener(new ClearItem(position,
		// node.getDeviceId()));

//		vh.img_state.setBackgroundResource(node.isOnline() ? R.drawable.img_state_online : R.drawable.img_state_offline);
		Bitmap map = listMap[position];
		if (map == null) {
//			vh.img.setImageDrawable(defaultImg);
		} else {
			vh.img.setImageBitmap(map);
		}
		// SoftReference<Bitmap> soft = getBitmap(node.getDeviceId());
		// if (soft != null) {
		// Bitmap bitmap = soft.get();
		// if (bitmap != null) {
		// vh.img.setImageBitmap(bitmap);
		// } else {
		//
		// vh.img.setImageDrawable(defaultImg);
		// }
		// } else {
		// soft = getBitmap(node.getDeviceId());
		// if (soft != null) {
		// Bitmap bitmap = soft.get();
		// if (bitmap != null) {
		// vh.img.setImageBitmap(bitmap);
		// } else
		// vh.img.setImageDrawable(defaultImg);
		// } else
		// vh.img.setImageDrawable(defaultImg);
		// }
		vh.tv.setText(node.getName());
		if (node.node.dwNodeId == currentNodeId) {
			vh.img.setBackgroundColor(colorSelector);

		} else {
			vh.img.setBackgroundColor(colorNormal);
		}
		return convertView;
	}

	// class ClearItem implements OnClickListener {
	// int position;
	// String deviceId;
	//
	// public ClearItem(int position, String deviceId) {
	//
	// this.position = position;
	// this.deviceId = deviceId;
	// }
	//
	// @Override
	// public void onClick(View v) {
	// // TODO Auto-generated method stub
	// // String name = nodeList.get(position).getName();
	// // String id = nodeList.get(position).getDeviceId();
	//
	// notifyDataSetChanged();
	//
	// }
	//
	// }

	class ViewHolder {
		TextView tv;
		ImageView img;
		ImageView img_state;
	}
}
